package com.sentinel.controller;

import com.sentinel.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @作者: 林江
 * @创建时间: 2023/12/15
 * @功能:
 */
@RestController
@RequestMapping("user")
public class UserController {
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Integer id) throws InterruptedException {
        if (id == 1) {
            // sentinel的熔断策略 -- 慢调用策略
//            TimeUnit.MILLISECONDS.sleep(60);
            return createUser(1, "林江");
        } else if (id == 2) {
            return createUser(2, "林仙");
        }
        // sentinel的熔断策略 -- 异常比例策略
        throw new RuntimeException("没有用户");
    }

    public User createUser(Integer id, String name) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }
}
